package com.boilerCloud.controller.project;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.device.ProjectInfo;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.service.device.IDeviceService;
import com.boilerCloud.service.device.IProjectService;
import com.boilerCloud.service.log.ILogService;

@RestController
@RequestMapping("/project/")
public class ProjectController {

	
	private static final Logger log = LoggerFactory.getLogger(ProjectController.class);
	@Resource
	private IProjectService projectService;

	@Resource
	private ILogService logService;

	@RequestMapping(value = "projectList")
	public ModelAndView projectList(HttpServletRequest request) {
		User user = (User)request.getSession().getAttribute("usersession");
		if(user.getRoleId() == 2 || user.getRoleId() == 3){//app 用户
			return new ModelAndView("/views/project/projectInfo_app.html");
		}
		return new ModelAndView("/views/project/projectInfo.html");
	}
	
	@RequestMapping(value = "projectMaps")
	public ModelAndView projectMaps() {
		return new ModelAndView("/views/project/projectInfoMap.html");
	}
	@RequestMapping(value = "deviceListTemp")
	public ModelAndView deviceListTemp(HttpServletRequest request) {
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		return new ModelAndView("/views/project/deviceListTemp.html?id=" + id);
	}

	/**
	 * 查询项目列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryProjectList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryItemList(@ModelAttribute ProjectInfo pro ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		System.out.println(pro.toString());
		try {
			HzlqswPage<ProjectInfo> page = new HzlqswPage<ProjectInfo>();
			page.setCurPage(yema);
			page.setPageSize(length);
			User user = (User)request.getSession().getAttribute("usersession");
			if(user.getRoleId() == 2){//app 用户
				pro.setUserId(user.getId());
			}
			if(user.getRoleId() == 3){//经销商
				pro.setDeviceId(user.getId());
			}
			projectService.qryList(pro,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询项目列表失败");
			log.error("查询项目列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改设备信息
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("修改项目信息")
	@RequestMapping(value = "editProject", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editDevice(
			@ModelAttribute ProjectInfo p ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			projectService.edit(p);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改设备信息失败");
			log.error("修改设备信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增项目
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增项目信息")
	@RequestMapping(value = "addProject", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addProject(
			@ModelAttribute ProjectInfo pro ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			 ProjectInfo pro1 = new ProjectInfo();
			 pro1.setProjectNo(pro.getProjectNo());
			 pro1.setIsDelete("n");
			 ProjectInfo info = projectService.getProjectInfo(pro1);
			if(info!=null){
				rst.setResultCode(ReqResult.resultCode_exist);
				rst.setReturnObject(pro.getProjectNo()+" 已存在,请重新输入");
				return rst;
			}
			 projectService.addProject(pro);
			 rst.setResultCode(ReqResult.resultCode_Success);
			 return rst;
		} catch (Exception e) {
			e.printStackTrace();
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增项目信息失败");
			log.error("新增项目信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除设备
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@LogAnnotation("删除项目信息")
	@RequestMapping(value = "delProject", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delDevice(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			projectService.delProject(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除设备失败");
			log.error("删除设备异常",e);
			return rst;
		}
	}
	
	
	/**
	 * @Title: getDeviceInfo
	 * @Description:(一句话阐述):查询单个对象的信息
	 * @param project
	 * @param request
	 * @return    
	 * @author Ran_cc
	 * @date 2018年3月8日 
	 */
	@RequestMapping(value = "getProjectInfo", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getDeviceInfo(
			@ModelAttribute ProjectInfo project ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			ProjectInfo pro = projectService.getProjectInfo(project);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(pro);
			return rst;
		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询项目失败");
			log.error("查询项目失败",e);
			return rst;
		}
	}
	
	
	

}
